package com.wanpinghui.wphlibrary.model.base;

import android.content.Context;

import com.bumptech.glide.load.data.DataFetcher;
import com.bumptech.glide.load.data.HttpUrlFetcher;
import com.bumptech.glide.load.model.GenericLoaderFactory;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.load.model.ModelLoader;
import com.bumptech.glide.load.model.ModelLoaderFactory;
import com.bumptech.glide.load.model.stream.StreamModelLoader;
import com.wanpinghui.wphlibrary.model.base.bean.CacheImage;

import java.io.InputStream;

/**
 * 功能描述
 * author:caitiangui
 * time:2017/12/13
 */

public class CacheImageLoader implements StreamModelLoader<CacheImage> {
    @Override
    public DataFetcher<InputStream> getResourceFetcher(final CacheImage model, int width,
                                                       int height) {
        return new HttpUrlFetcher(new GlideUrl(model.getImageUrl())) {
            @Override
            public String getId() {
                return model.getImageId();
            }
        };
    }

    public static class Factory implements ModelLoaderFactory<CacheImage, InputStream> {
        @Override
        public ModelLoader<CacheImage, InputStream> build(Context context,
                                                          GenericLoaderFactory factories) {
            return new CacheImageLoader();
        }

        @Override
        public void teardown() { /* no op */ }
    }
}

